package com.frank.oj.service.admin.discussion.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.frank.model.entity.discussion.Discussion;
import com.frank.model.entity.discussion.DiscussionReport;
import com.frank.oj.common.exception.StatusFailException;
import com.frank.oj.common.result.CommonResult;
import com.frank.oj.manager.AdminNoticeManager;
import com.frank.oj.model.vo.DiscussionReportVO;
import com.frank.oj.service.admin.discussion.AdminDiscussionService;
import com.frank.oj.service.entity.discussion.DiscussionEntityService;
import com.frank.oj.service.entity.discussion.DiscussionReportEntityService;
import com.frank.oj.shiro.AccountProfile;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author frank
 * @Date 2024/4/7
 */
@Service
public class AdminDiscussionServiceImpl implements AdminDiscussionService {
    @Resource
    private DiscussionReportEntityService discussionReportEntityService;
    @Resource
    private DiscussionEntityService discussionEntityService;
    @Resource
    private AdminNoticeManager adminNoticeManager;

    @Override
    public IPage<DiscussionReportVO> getDiscussionReport(Integer currentPage, Integer limit) {

        return discussionReportEntityService.getDiscussionReportList(currentPage, limit);

    }

    @Override
    public void updateDiscussionReport(DiscussionReport discussionReport) throws StatusFailException {
        boolean updated = discussionReportEntityService.updateById(discussionReport);
        if (!updated) {
            throw new StatusFailException("更新失败");
        }
    }

    @Override
    public void updateDiscussion(Discussion discussion) throws StatusFailException {
        boolean updated = discussionEntityService.updateById(discussion);
        if (!updated) {
            throw new StatusFailException("更新失败");
        }
        // 封禁则发送通知
        if (discussion.getStatus()==1){
            //LambdaQueryWrapper<DiscussionReport> queryWrapper = new LambdaQueryWrapper<>();
            //queryWrapper.eq(DiscussionReport::getDid,discussion.getId());
            //List<DiscussionReport> reports = discussionReportEntityService.list(queryWrapper);
            AccountProfile userRole = (AccountProfile) SecurityUtils.getSubject().getPrincipal();
            adminNoticeManager.addSingleNoticeToUSer(userRole.getUid(), discussion.getUid(),
                    "评论/讨论封禁通知",
                    "您发布的评论/讨论【"+discussion.getTitle()+"】存在违规行为，已被管理员"+userRole.getUsername()+"封禁，详情请联系管理员！","Single");
        }
    }

    @Override
    @Transactional
    public void deleteDiscussion(List<Integer> didList) throws StatusFailException {

        // 删除对应的举报信息
        LambdaQueryWrapper<DiscussionReport> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(DiscussionReport::getDid, didList);

        boolean rRemoved = discussionReportEntityService.remove(queryWrapper);
        boolean removed = discussionEntityService.removeByIds(didList);
        if (!rRemoved || !removed) {
            throw new StatusFailException("删除失败");
        }

    }
}
